package com.troyhigh.njrotc.admintrackerold;

import java.util.Properties;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

public class AdminTracker
{
    private Class DB_MODEL;
    private Database database;
    
    public AdminTracker()
    {
        this(SQLDatabaseAL.class);
    }
    
    public AdminTracker(Class dbm)
    {
        DB_MODEL = dbm;
        try 
        {
            System.out.println("Database model: " + DB_MODEL.toString());
            database = ((Database) DB_MODEL.newInstance());
        }
        catch (Exception e) 
        {
            System.out.println("Class Instantiation Exception!");
        }
        database.connect();
    }
    
    public void close()
    {
        database.disconnect();
    }
      
    public Event createEvent(String name, String date, String category, String counting, int countingvalue)
    {
        return createEvent(name, Date.valueOf(date), Category.parseCategory(category), counting, countingvalue);
    }
    
    public Event createEvent(String n, Date d, Category c, String co, int cv)
    {
        if(database.getEvent(n, d) == null)
        {
            Properties props = new Properties();
            props.setProperty("counting", co);
            props.setProperty("countingvalue", "" + cv);
            Event result = new Event(n, d, c, props);
            database.setEvent(result);
            return result;
        }
        return null;
    }
    
    public Student createStudent(int group, String ln, String fn, int id, String gen, int grade, int nsyear)
    {
        if(database.getStudent(ln, fn, id) == null)
        {
            Properties props = new Properties();
            props.setProperty("gender", gen);
            props.setProperty("grade", ""+grade);
            props.setProperty("nsyear", ""+nsyear);
            Student result = new Student(group, ln, fn, id, props);
            database.setStudent(result);
            return result;
        }
        return null;
    }
    
    public ArrayList<Event> getEvents()
    {
        return database.getEvents();
    }
    
    public ArrayList<Event> getEvents(String n, Date d)
    {
        return database.getEvents(n, d);
    }
    
    public ArrayList<Student> getStudents()
    {
        return database.getStudents();
    }
    
    public ArrayList<Student> getStudents(String l, String f, int id)
    {
        return database.getStudents(l, f, id);
    }
    
    public Event getEvent(String name, Date d)
    {
        return database.getEvent(name, d);
    }
    
//    public Event getEvent(String name, String d)
//    {
//        return database.getEvent(name, Date.valueOf(d));
//    }
    
    public Student getStudent(String ln, String fn, int id)
    {
        return database.getStudent(ln, fn, id);
    }
    
    public void removeStudent(String ln, String fn, int id)
    {
        database.removeStudent(ln, fn, id);
    }
    
    public void removeEvent(String name, Date d)
    {
        database.removeEvent(name, d);
    }
    
    public RibbonData getAward(int id)
    {
        return database.getAward(id);
    }
    
    public ArrayList<RibbonData> getAwards()
    {
        return database.getAwards();
    }
    
    public void setAward(RibbonData r)
    {
        database.setAward(r);
    }
    
    public void setAwards(ArrayList<RibbonData> r)
    {
        database.setAwards(r);
    }
    
    public ArrayList<Student> getEventStudents(Event e)
    {
        ArrayList<Student> st = new ArrayList<Student>();
        ArrayList<Integer> id = e.getIDs();
        for(Integer i : id)
        {
            st.add(database.getStudent("", "", i.intValue()));
        }
        return st;
    }
    
    public int getStudentTotal(Student s, Category c)
    {
        int result = 0;
        if(c.toString().equals("PARTICIPATION"))
        {
            ArrayList<Event> es = database.getEvents();
            for(Event e : es)
            {
                if(e.get(s) > 0)
                      result++;
            }
            return result;
        }
        ArrayList<Event> es = database.getEvents();
        for(Event e : es)
        {
            if(e.getCategory().equals(c))
                 result += e.get(s);
        } 
        return result;
    }
    
    public int getStudentAward(Student s, Category c)
    {
        int count = getStudentTotal(s, c);
        count += s.getRibbons().get(c);
        int award = Category.calculateAward(c, count);
        int ns = Integer.parseInt(s.getProperties().getProperty("nsyear", "1"));
        int cap = (2 * ns) - ((isSecondSemester()) ? 0 : 1);
        return ((award <= cap) ? award : cap);
    }
    
    public ArrayList<Event> getStudentEvents(Student s)
    {
        ArrayList<Event> es = database.getEvents();
        ArrayList<Event> re = new ArrayList<Event>();
        for(Event e : es)
        {
            if(e.get(s.getID()) > 0)
                re.add(e);
        }
        return re;
    }
    
    public ArrayList<String> getAward(Category c)
    {
        ArrayList<String> result = new ArrayList<String>();
        ArrayList<Student> students = database.getStudents();
        for(Student s : students)
        {
            int a = getStudentAward(s, c);
            if(a > 0)
            {
                String text = ("Platoon " + s.getGroup() + ", Student " + s.getName() + ", ID " + s.getID() + "" +
                           "," + c + " " +  Category.awardAmount(a)) + ".";
                result.add(text);
            }
        }
        Collections.sort(result);
        return result;
    }
    
    private boolean isSecondSemester()
    {
        java.util.Date now = new java.util.Date(), midterm = new java.util.Date(), start = new java.util.Date();
        now = new java.util.Date();  
        int year = Calendar.getInstance().get(Calendar.YEAR);
        midterm = ((java.util.Date)Date.valueOf(""+year+"-01-31"));
        start = ((java.util.Date)Date.valueOf(""+year+"-07-31"));
        return (now.after(midterm) && now.before(start));
    }   
    
    public void addCredit(Event e, Student s, int v)
    {
        addCredit(e, s.getID(), v);
    }
    
    public void addCredit(Event e, int s, int v)
    {
        if(e != null && s > 0 && v > 0 && database.eventExists(e))
        {
            e.add(s, v);
            database.setRoster(e);
        }
    }
    
    public void removeCredit(Event e, Student s)
    {
        if(e != null && s != null && database.eventExists(e) && database.studentExists(s))
        {
            e.remove(s);
            database.setRoster(e);
        }
    }
    
    public Database getDatabase()
    {
        return database;
    }
}
